#include "DynamicArray.h"
#include <MyString.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct Student
{
    int id;
    MyStr name;
    int age;
};
typedef struct Student Stu;

Stu * CreateStu(const int id,const char *name,const int age)
{
    Stu *s = (Stu *)malloc(sizeof(Stu));
    if(s == NULL)
    {
        return NULL;
    }
    s->id = id;
    s->age = age;
    s->name = GetString(name);
    return s;
}

void ClearStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    //printf("释放学生对象%s\n",s->name.string);
    ClearString(&s->name);
    free(s);
}

void PrintStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    printf("学生学号:%d 学生姓名:%s 学生年龄:%d\n",s->id,s->name.string,s->age);
}

bool SortByAge(void *ptr1, void *ptr2)
{
    Stu *s1 = (Stu*)ptr1;
    Stu *s2 = (Stu*)ptr2;
    if(s1->age > s2->age)
    return true;
    return false;
}

bool SortByName(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu*)ptr1;
    Stu *s2 = (Stu*)ptr2;
    if(strcmp(s1->name.string , s2->name.string) > 0)
    return true;
    return false;
}

bool SortById(void *ptr1, void *ptr2)
{
    Stu *s1 = (Stu*)ptr1;
    Stu *s2 = (Stu*)ptr2;
    if(s1->id > s2->id)
        return true;
    return false;
}

bool CheckAge(void *ptr1,void *ptr2)
{
    long age = (long)ptr1;
    Stu *s = (Stu*)ptr2;
    if(s->age >= age)
    {
        return true;
    }
    return false;
}

struct Teacher
{
    MyStr name;
    int age;
    DArray StuArray;//学生数组
};
typedef struct Teacher Teach;

Teach *CreateTeach(const char *name,int age)
{
    Teach *t = (Teach*)malloc(sizeof(Teach));
    if(t == NULL)
    {
        return NULL;
    }
    t->age = age;
    t->name = GetString(name);
    InitDynamicArray(&t->StuArray);
    return t;
}

void ClearTeach(void *ptr)
{
    Teach *t = (Teach*)ptr;
    ClearString(&t->name);
    //清空数组
    t->StuArray.ClearDynamicArray(&t->StuArray,ClearStu);
    free(t);
}

void AddStudent(Teach *t,Stu *s)
{
    t->StuArray.InsertArrayTail(&t->StuArray,s);
}

void PrintTeach(void *ptr)
{
    Teach *t = (Teach*)ptr;
    printf("老师名字:%s 老师年龄:%d\n",t->name.string,t->age);
    printf("老师的学生的列表:\n");
    t->StuArray.DisplayDynamicArray(&t->StuArray,PrintStu);
}

bool CheckNameContain(void *ptr1, void *ptr2)
{
    char *name = (char *)ptr1;
    Teach *t = (Teach *)ptr2;

    if(IsContains(&t->name,name) == true)
        return true;
    return false;
}


int main()
{
    // DArray a;
    // InitDynamicArray(&a);
    
        // for(int i = 0; i < 10; i++)
    // {
    //     InsertTail(&a,(void*)i+1);
    // }

    
    // InsertArrayTail(&a,CreateStu(1,"zhangsan",20));
    // InsertArrayTail(&a,CreateStu(2,"lisi",22));
    // InsertArrayTail(&a,CreateStu(3,"wangwu",21));

    // Sort(&a,SortByAge);
    // DisplayDynamicArray(&a,PrintStu);

    // RemoveByElement(&a,(void *)21,CheckAge,ClearStu);
    // printf("删除后:\n");
    // DisplayDynamicArray(&a,PrintStu);
    // ClearDynamicArray(&a,ClearStu);

    // DArray TeachArray;
    // InitDynamicArray(&TeachArray);  //防止出现垃圾值
    INITDARRAY(TeachArray);
    TeachArray.InsertArrayTail(&TeachArray,CreateTeach("张三",40));
    TeachArray.InsertArrayTail(&TeachArray,CreateTeach("李四",50));

    // Teach *t = CreateTeach("张老师",40);
    // AddStudent(t,CreateStu(1,"zhangsan",20));
    // AddStudent(t,CreateStu(2,"lisi",22));
    // AddStudent(t,CreateStu(3,"wangwu",21));
    // PrintTeach(t);

    // Teach *t1 = CreateTeach("李老师",42);
    // AddStudent(t1,CreateStu(1,"xiaoming",21));
    // AddStudent(t1,CreateStu(2,"xiaohong",23));
    // AddStudent(t1,CreateStu(3,"xiaogang",25));
    // PrintTeach(t1);
    // InsertArrayTail(&TeachArray,t);
    // InsertArrayTail(&TeachArray,t1);

    DArray arr = TeachArray.FindElement(&TeachArray,(void *)"张",CheckNameContain);
    TeachArray.DisplayDynamicArray(&arr,PrintTeach);
    

    //DisplayDynamicArray(&TeachArray,PrintTeach);
    TeachArray.ClearDynamicArray(&TeachArray,ClearTeach);

    // ClearTeach(t);
    // ClearTeach(t1);
    return 0;
}